package org.jash.demo2205.utils

import com.alibaba.android.arouter.utils.TextUtils
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

private val client:OkHttpClient by lazy {
    OkHttpClient.Builder()
        .callTimeout(30, TimeUnit.SECONDS)
        .connectTimeout(30, TimeUnit.SECONDS)
        .readTimeout(30, TimeUnit.SECONDS)
        .writeTimeout(30, TimeUnit.SECONDS)
        .addInterceptor { chain ->
            val token = SPUtils.instance.getString("token", "")
            val request = if (TextUtils.isEmpty(token)) chain.request() else chain.request().newBuilder().header("token", token).build()
            chain.proceed(request)
        }
        .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
        .build()
}

val retrofit:Retrofit by lazy {
    Retrofit.Builder()
        .baseUrl("http://10.161.9.80:7012/")
        .client(client)
        .addConverterFactory(GsonConverterFactory.create())
        .build()
}